<?php
/**
 * Created by PhpStorm.
 * User: wuchunyin
 * Date: 2018/11/8
 * Time: 13:36
 */

namespace app\index\controller;
use app\index\base_controller\BaseController;
use app\index\table\IntangibleTable;
use think\Exception;
use think\Db;

class Intangible extends BaseController{

    /**
     * page--基本信息
     */
    public function intangibleList(){
        return view('intangible_list');
    }

    /**
     * action--获取信息
     */
    public function getIntangibleList(){
        $limit = input('limit');
        $offset = input('offset');
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";
        if(empty($sort)){
            $sortStr = 'INTANG_NO desc';
        }

        $condition = null;
        $listCondition = Login::getListConditionIntangible();
        if (!empty($listCondition['INTANG_NO'])) {
            $value = $listCondition['INTANG_NO'];
            $condition[] = ['INTANG_NO','like',"$value%"];
        }
        if (!empty($listCondition['INTANG_NAME'])) {
            $value = $listCondition['INTANG_NAME'];
            $condition[] = ['INTANG_NAME','like',"%$value%"];
        }
        if (!empty($listCondition['ASSET_TYPE_ID'])) {
            $value = $listCondition['ASSET_TYPE_ID'];
            $condition[] = ['ASSET_TYPE_ID','=',$value];
        }
        if (!empty($listCondition['DEPART_ID'])) {
            $value = $listCondition['DEPART_ID'];
            $condition[] = ['DEPART_ID','=',$value];
        }
        if (!empty($listCondition['GETTYPE_ID'])) {
            $value = $listCondition['GETTYPE_ID'];
            $condition[] = ['GETTYPE_ID','=',$value];
        }
        if (!empty($listCondition['USEDIRECTION_ID'])) {
            $value = $listCondition['USEDIRECTION_ID'];
            $condition[] = ['USEDIRECTION_ID','=',$value];
        }
        if (!empty($listCondition['STATUS_ID'])) {
            $value = $listCondition['STATUS_ID'];
            $condition[] = ['STATUS_ID','=',$value];
        }
        if (!empty($listCondition['VALUE_START'])) {
            $value = $listCondition['VALUE_START'];
            $condition[] = ['VALUE','>=',$value];
        }
        if (!empty($listCondition['VALUE_END'])) {
            $value = $listCondition['VALUE_END'];
            $condition[] = ['VALUE','<=',$value];
        }

        if (!empty($listCondition['GET_DATE_START'])) {
            $value = Db::raw("to_date('{$listCondition['GET_DATE_START']}','yyyy-mm-dd')");
            $condition[] = ['GET_DATE','>=',$value];
        }
        if (!empty($listCondition['GET_DATE_END'])) {
            $value = Db::raw("to_date('{$listCondition['GET_DATE_END']}','yyyy-mm-dd')");
            $condition[] = ['GET_DATE','<=',$value];
        }

        if (!empty($listCondition['ENTER_TIME_START'])) {
            $value = Db::raw("to_date('{$listCondition['ENTER_TIME_START']}','yyyy-mm-dd')");
            $condition[] = ['ENTER_TIME','>=',$value];
        }
        if (!empty($listCondition['ENTER_TIME_START'])) {
            $value = Db::raw("to_date('{$listCondition['ENTER_TIME_START']}','yyyy-mm-dd')");
            $condition[] = ['ENTER_TIME','<=',$value];
        }

        try{
            $list = IntangibleTable::table()->where($condition)->order($sortStr)->limit($offset,$limit)->select();
            $total = IntangibleTable::table()->where($condition)->count();
            return ['rows'=>$list,'total'=>$total];
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * page 新增
     */
    public function addPage(){
        $config = IntangibleTable::getConfigAdd();
        $this->assign('config',$config);
        $this->assign('data',null);
        $this->view->engine->layout('layout_popup');
        return view('intangible_add');
    }

    /**
     * action 新增
     */
    public function add(){
        $data = input();
        try{
            IntangibleTable::add($data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * page 编辑
     */
    public function editPage(){
        $intangNo = input('intangNo');
        $data = IntangibleTable::get($intangNo);
        if(empty($data)){
            return '查询无形资产信息失败';
        }
        $config = IntangibleTable::getConfigEdit();
        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('intangible_edit');
    }

    /**
     * action 编辑
     */
    public function edit(){
        $intangNo = input('INTANG_NO');
        $data = input();
        try{
            IntangibleTable::update(['INTANG_NO'=>$intangNo],$data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * action 批量删除
     */
    public function deleteAll(){
        $list = input('list');
        Db::startTrans();
        try{
            foreach ($list as $intangNo){
                IntangibleTable::delete(['INTANG_NO'=>$intangNo]);
            }
            Db::commit();
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            Db::rollback();
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * page 导出Excel
     */
    public function exportExcel(){
        $search = input('search');
        $sortStr = 'INTANG_NO desc';
        $condition = null;
        if (!empty($search)) {
            $condition[] = ['INTANG_NO|INTANG_NAME','like',"%$search%"];
        }

        try{
            $list = IntangibleTable::table()->where($condition)->order($sortStr)->select();
            IntangibleTable::exportExcel($list,'无形资产');
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * 打印
     */
    public function printPage() {
        $id = input('id');
        $data = IntangibleTable::get($id);
        if(empty($data)){
            return '查询无形资产信息失败';
        }
        $config = IntangibleTable::getConfigPrint();
        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('intangible_print');
    }

    /**
     * page--高级查询
     */
    public function searchPage(){
        $this->view->engine->layout('layout_popup');
        return view('intangible_search');
    }

    public function addConditionSession() {
        $condition = input();
        Login::setListConditionIntangible($condition);
        return json_encode(['code'=>'200']);
    }

    public function importPage() {
        $this->view->engine->layout('layout_popup');
        return view('import');
    }

    public function import() {
        $inputData = input('data');
        $postData = base64_decode($inputData);
        $list = json_decode($postData,true);
        $start = input('start');

        Db::startTrans();
        $fieldList = IntangibleTable::INTANGIBLE_FIELD_LIST;
        $fieldCount = count($fieldList);
        foreach ($list as $key => $row) {
            $data = [];

            // 字段赋值
            $i = 0;
            foreach ($row as $key1=>$value) {
                $data[$fieldList[$i]] = $value;
                $i++;
                if ($i>=$fieldCount) {
                    break;
                }
            }

            try{
                IntangibleTable::addImport($data);
            }
            catch (Exception $e){
                Db::rollback();
                return json_encode(['code'=>'0','msg'=>"第".($key + $start)."行数据写入出错",'data'=>$e->getMessage()]);
            }
        }

        Db::commit();
        return json_encode(['code'=>'200','msg'=>'导入成功']);
    }



}